// pages/adviser/chat/chat.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    InputBottom: 0,
    adviesr_id: null,
    content: '',
    message_info: null,
    useropen_id:wx.getStorageSync("user_new"),
    inputValue:''
  },
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      adviesr_id: options.id
    })
    let user_id = wx.getStorageSync('user_new')
    let adviesr_id = this.data.adviesr_id
    app.core('get', 'getMessageWithOne?from_id=' + user_id + '&to_id=' + adviesr_id,'',(res) => {
      console.log(res)
      this.setData({
        message_info:res.data
      })
      console.log(this.data.message_info)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 留言
  chatVal(e) {
    let content = e.detail.value
    // console.log(content)
    this.setData({
      content
    })
  },
  // 发送留言
  sendChat() {
    let from_id = wx.getStorageSync('user_new')
    let to_id = this.data.adviesr_id
    let userInfo = wx.getStorageSync('userInfo')
    let name = userInfo.nickName
    let headpic = userInfo.avatarUrl
    let content = this.data.content
    if(content === '') {
      wx.showToast({
        title: '留言内容不能为空',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    app.core('post', 'saveMessage', { from_id, to_id, name, headpic, content},(res) => {
      console.log(res.data)
      this.data.message_info.push(res.data)
      this.setData({
        message_info:this.data.message_info,
        inputValue:''
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})